package com.example.springcache.service;

import org.springframework.cache.annotation.*;

@CacheConfig(cacheNames = {"myCache"})
public class CacheService {

    @Cacheable(key = "targetClass + methodName +#p0")//只查询不更新,此处没写value，在类名上。
    public Object findAll(int num) {
        return null;
    }

    @CachePut(value = "emp", key = "targetClass + #p0")//更新
    public Object updata(int job) {
        return null;
    }

    @Cacheable(value = "emp", key = "targetClass +#p0")
    public Object save(int job) {
        return null;
    }

    @Cacheable(value = "emp",key = "#p0.id")
    public Object save2(int job) {
        return null;
    }

    //清除一条缓存，key为要清空的数据
    @CacheEvict(value="emp",key="#id")
    public void delect(int id) {
        //  deleteAllById(id);
    }

    //方法调用后清空所有缓存
    @CacheEvict(value="accountCache",allEntries=true)
    public void delectAll() {
        //  deleteAll();
    }

    //方法调用前清空所有缓存
    @CacheEvict(value="accountCache",beforeInvocation=true)
    public void delectAll2() {
        //  deleteAll();
    }

    @Caching(cacheable = {
            @Cacheable(value = "emp",key = "#p0")},
            put = {@CachePut(value = "emp",key = "#p0")},
            evict = {@CacheEvict(value = "emp",key = "#p0")})
    public Object save3(int id) {
        return null;
    }

}